<?php

namespace App\Http\Controllers\Admin;


use App\Http\Models\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    public function index(){
        return view('admin.index');
    }
    public function info(){
        return view('admin.info');
    }

    // 超级管理员密码修改
    public function pass(){
        // POST 请求 更新密码
        if($input=Input::all()){
           // 验证原始密码是否正确
           // 验证新密码两次是否一致
           // 新密码长度6-20位
            $rules = [
                'user_pass'=>'required|between:6,20|confirmed',
            ];
            $message = [
                'user_pass.required'=>'新密码不能为空!',
                'user_pass.between'=>'密码必须在6到20位之间!',
                'user_pass.confirmed'=>'两次密码必须一致!',
            ];
           $validator = Validator::make($input,$rules,$message);
           if($validator->passes()){
                //验证规则通过
                //验证原始密码是否正确
               $old_pass = $input['pass_o'];
               $new_pass = $input['user_pass'];
               $field = User::first();
                if($old_pass == decrypt($field->user_pass)){
                   $field->user_pass = encrypt($new_pass);
                   $res = $field->update();
                   if($res){
                       return back()->with(['errors'=>'密码修改成功!']);
                   }else{
                       return back()->with(['errors'=>'密码修改失败,请稍后再试!']);
                   }
                }
               return back()->with('errors','原始密码不正确!');
           }else{
               // 验证规则不通过
             return back()->withErrors($validator);
           }

        }else{
            // get 请求
            return view('admin.pass');
        }

    }
}
